#include "stm32f10x.h"                  // Device header
#include "MPU6050_Reg.h"


#define MPU6050_ADDRESS    0xD0


/*
@brief: 等待事件超时函数
@param: I2C_TypeDef* I2Cx
@param: 等待的事件
@revatal: 无
*/
void MPU6050_WaitEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT)
{
	uint32_t Timeout;			// 简单的计次计时
	Timeout = 10000;
	while(I2C_CheckEvent(I2C2, I2C_EVENT) == ERROR)
	{
		Timeout --;
		if (Timeout == 0)
		{
			break;
		}
	}
}

/*
@brief: 写入数据
@param: 寄存器地址
@param: 要写入的数据
@revatal: 无
*/
void MPU6050_WriteReg(uint8_t Reg_ADDRESS, uint8_t Data)
{
	I2C_GenerateSTART(I2C2, ENABLE);												// 主机模式选择
	
	I2C_Send7bitAddress(I2C2, MPU6050_ADDRESS, I2C_Direction_Transmitter); 			// 发送(自带应答过程),出错有提示
	MPU6050_WaitEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED);// 发送的EV6

	I2C_SendData(I2C2, Reg_ADDRESS);
	MPU6050_WaitEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTING); 	// EV8
	
	I2C_SendData(I2C2, Data);
	MPU6050_WaitEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED);		// EV8_2

	I2C_GenerateSTOP(I2C2, ENABLE);
}

/*
@brief: 读取数据
@param: 寄存器地址
@revatal: 读取的数据
*/
uint8_t MPU6050_ReadReg(uint8_t Reg_ADDRESS)
{
	uint8_t Data;
	
	I2C_GenerateSTART(I2C2, ENABLE);												// 主机模式选择
	MPU6050_WaitEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT);				// EV5
	
	I2C_Send7bitAddress(I2C2, MPU6050_ADDRESS, I2C_Direction_Transmitter); 			// 发送(自带应答过程),出错有提示
	MPU6050_WaitEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED);// EV6

	I2C_SendData(I2C2, Reg_ADDRESS);
	MPU6050_WaitEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED);		// 改为 TRANSMITTED

	I2C_GenerateSTART(I2C2, ENABLE);
	MPU6050_WaitEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT);				// 重复启动
	
	I2C_Send7bitAddress(I2C2, MPU6050_ADDRESS, I2C_Direction_Receiver); 			
	MPU6050_WaitEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED);  // 读取的EV6
	
	// 因为这是单个字节的情况, 提前给非应答, 提前设置停止位
	I2C_AcknowledgeConfig(I2C2, DISABLE);
	I2C_GenerateSTOP(I2C2, ENABLE);
	
	MPU6050_WaitEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED);  			// EV7
	Data = I2C_ReceiveData(I2C2);

I2C_AcknowledgeConfig(I2C2, ENABLE);				// 最后别忘了这个, 不然后面的应答一直是0

	return Data;
}

/*
@brief: MPU6050初始化, 建立在I2C上的
@param: 无
@revatal: 无
*/
void MPU6050_Init(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);						//将PA1和PA2引脚初始化为推挽输出

	I2C_InitTypeDef I2C_InitStruture;
	I2C_InitStruture.I2C_Ack = I2C_Ack_Enable;
	I2C_InitStruture.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	I2C_InitStruture.I2C_ClockSpeed = 50000;
	I2C_InitStruture.I2C_DutyCycle = I2C_DutyCycle_2;
	I2C_InitStruture.I2C_Mode = I2C_Mode_I2C;
	I2C_InitStruture.I2C_OwnAddress1 = 0X00;
	I2C_Init(I2C2, &I2C_InitStruture);
	
	I2C_Cmd(I2C2, ENABLE);
}

/*
@brief: 读取MPU6050加速度计和陀螺仪的数据
@param: X,Y,Z轴 的 加速度&角速度
@revatal: 无
*/
void MPU6050_GetData(int16_t *AccX, int16_t *AccY, int16_t *AccZ, 
					int16_t *GyroX, int16_t *GyroY, int16_t *GyroZ)
{
	// 分别读取6个轴数据寄存器的高位和地位, 拼接成16位的数据, 再
	// 通过指针变量返回
	uint8_t DataH, DataL;
	DataH = MPU6050_ReadReg(MPU6050_ACCEL_XOUT_H);
	DataL = MPU6050_ReadReg(MPU6050_ACCEL_XOUT_L);
	*AccX = (DataH << 8) | DataL;

	DataH = MPU6050_ReadReg(MPU6050_ACCEL_YOUT_H);
	DataL = MPU6050_ReadReg(MPU6050_ACCEL_YOUT_L);
	*AccY = (DataH << 8) | DataL;

	DataH = MPU6050_ReadReg(MPU6050_ACCEL_ZOUT_H);
	DataL = MPU6050_ReadReg(MPU6050_ACCEL_ZOUT_L);
	*AccZ = (DataH << 8) | DataL;
	
	DataH = MPU6050_ReadReg(MPU6050_ACCEL_XOUT_H);
	DataL = MPU6050_ReadReg(MPU6050_ACCEL_XOUT_L);
	*GyroX = (DataH << 8) | DataL;

	DataH = MPU6050_ReadReg(MPU6050_ACCEL_YOUT_H);
	DataL = MPU6050_ReadReg(MPU6050_ACCEL_YOUT_L);
	*GyroY = (DataH << 8) | DataL;

	DataH = MPU6050_ReadReg(MPU6050_ACCEL_ZOUT_H);
	DataL = MPU6050_ReadReg(MPU6050_ACCEL_ZOUT_L);
	*GyroZ = (DataH << 8) | DataL;
}

/*
@brief: 读取MPU6050设备的ID号
@param: 
@revatal: ID
*/
uint8_t MPU6050_GetID(void)
{
	return MPU6050_ReadReg(MPU6050_WHO_AM_I);
}
